﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Metriks.Parsiranje.Klase;
namespace Metriks.Refactoring
{
    public class Iskazni
    {
        public static List<LOC> iskazniNivo(List<LOC> lista){
            bool usaoIf=false;
            bool usaoElse=false;
            bool vrijemeCekiranja=false;
            List<LOC> pom = new List<LOC>();
            List<LOC> pom1 = new List<LOC>();
            Stack<LOC> stek = new Stack<LOC>();
            for (int i = 0; i < lista.Count; i++) { 
                if(lista[i] is beginIf){
                    usaoIf = true;
                    continue;
                }
                if (lista[i] is endIf && !usaoElse) {
                    usaoIf = false;
                    pom = new List<LOC>();
                    pom1 = new List<LOC>();
                    continue;
                }
                if (lista[i].GetType() != typeof(beginElse) && usaoIf) {
                    pom.Add(lista[i]);
                    continue;
                }
                if (lista[i] is beginElse) {
                    usaoIf = false;
                    usaoElse = true;
                }
                if (lista[i].GetType() != typeof(beginElse) && usaoElse) {
                    pom1.Add(lista[i]);
                    continue;
                }
                if (lista[i] is endIf && usaoElse) {
                    usaoElse = false;
                    vrijemeCekiranja = true;
                }
                if (vrijemeCekiranja) { 
                    foreach(LOC l in pom){
                        stek.Push(pom1.Find(l.Equals));
                    }
                    if (stek.Count!=0) {
                        while (stek.Count != 0) {
                            LOC element = stek.Pop();
                            lista.RemoveAll(element.Equals);
                            lista.Insert(i, element);
                        }
                        
                    }
                    vrijemeCekiranja = false;
                }  
            }
            return lista;
        }

    }
}
